/**   
 * @Title: OrgDataAction.java 
 * CopyRight(c)2010-DMX 
 */
package com.dmx.fw.web.struts2.action.system;

import java.io.IOException;
import java.util.Date;

import org.apache.log4j.Logger;

import com.dmx.fw.bean.mapping.pojo.Organization;
import com.dmx.fw.service.interfaces.OrgDataService;
import com.dmx.fw.web.struts2.action.BaseAction;

/** 
 * @Description: 某一组织机构的项增、删、改值Action
 * @author: zhaojie
 * @date: 2010.4
 * @version V1.0 
 * 
 */
public class OrgDataAction extends BaseAction{
	private static Logger log = Logger.getLogger(OrgDataAction.class);

	private OrgDataService orgDataService;
	
	private Organization paramData;
	
	
	//执行添加提交操作
	public String doAddSubmit() throws Exception{
		String retMsg = this.savaParamData();
		if (retMsg == null){
			String resultUrl = "system/orgManager.action?act=query&busiId=searchOrg";
			this.setSuccessinfo(this.getText("info.addSuccess"));
			
			this.setAjaxJsonUrlResult(resultUrl);
		}
		else{
			this.setAjaxJsonMsgResult(retMsg,null);
		}
		return null;    
	}
	
	//执行修改提交界面
	public String doEditSubmit() throws Exception{
		String retMsg = this.updateParamData();
		if (retMsg == null){
			String resultUrl = "system/orgManager.action?act=query&busiId=searchOrg";
			this.setSuccessinfo(this.getText("info.modifySuccess"));
			this.setAjaxJsonUrlResult(resultUrl);
		}
		else{
			this.setAjaxJsonMsgResult(retMsg,null);
		}
		return null;
	}
	
	//执行删除操作
	public String doDelete() throws IOException{
		Organization param = new Organization();
		param.setOrgId(paramData.getOrgId());
		orgDataService.delete(param);
		String resultUrl = "system/orgManager.action?act=query&busiId=searchOrg";
		this.setSuccessinfo(this.getText("info.deleteSuccess"));
		this.setAjaxJsonUrlResult(resultUrl);
		return null;
	}
	
	/**
	 * 添加项
	 * @return
	 */
	private String savaParamData() {
		if (orgDataService.uniqueCheck(paramData.getOrgId().toString())){
			Organization param = new Organization();
			param.setOrgId(paramData.getOrgId());
			param.setParOrgId(paramData.getParOrgId());
			param.setName(paramData.getName());
			param.setStatus(paramData.getStatus());
			param.setCreatedTime(new Date());
			orgDataService.create(param);
			return null;
		}
		else{
			return getText("message.Conflict");
		}
	}

	/**
	 * 修改项
	 * @return
	 */
	private String updateParamData() {
		Organization param = orgDataService.getOrganization(paramData.getOrgId().toString());
		param.setOrgId(paramData.getOrgId());
		param.setParOrgId(paramData.getParOrgId());
		param.setName(paramData.getName());
		param.setStatus(paramData.getStatus());
		param.setCreatedTime(new Date());
		orgDataService.update(param);
		return null;
	}

	public OrgDataService getOrgDataService() {
		return orgDataService;
	}

	public void setOrgDataService(OrgDataService orgDataService) {
		this.orgDataService = orgDataService;
	}
	
	public Organization getParamData() {
		return paramData;
	}

	public void setParamData(Organization paramData) {
		this.paramData = paramData;
	}

}
